var fs = require('fs');
var mysql = require('mysql');
var superagent = require('superagent');
var cheerio = require('cheerio');

require('superagent-proxy')(superagent);

var mysqlConfig1 = require('./config').mysqlConfig1;
var mysqlConfig2 = require('./config').mysqlConfig2;

var ipList = [];
var ipIndex = 0;
var proxy = '';

function changeIp() {
    if (ipIndex < ipList.length - 1) {
        ipIndex += 1;
    } else {
        ipIndex = 0;
    }
    setProxy();
}

function setProxy() {
    var item = ipList[ipIndex];
    console.log(item);

    var ip = item.ip;
    var port = item.port;

    proxy = `http://${ip}:${port}`;
    console.log('proxy');

    console.log(proxy);

}

var connection1 = mysql.createConnection(mysqlConfig2);
var query1 = 'select * from ip_table1 where (success_count>5 and error_count/success_count<0.8 )';
connection1.connect();
connection1.query(query1, function (err, res) {
    if (err) {
        console.log('query1 error');

    } else {
        ipList = res;
        console.log(ipList.length);
        setProxy();
        console.log(proxy);
        setInterval(() => {
            changeIp();
        }, 300 * 1000);
        for (var i = 0; i < 10; i++) {
            var offset = 10 * i;
            fn1(offset);
        }
        // fn1(0);

    }
});
connection1.end();

function fn1(offset) {
    var artistList = [];
    var artistIndex = 0;

    var artistListUpdated = [];
    var albumList = [];
    var connection2 = mysql.createConnection(mysqlConfig1);
    var query2 = 'select * from artist where ISNULL( album_updated ) limit 3 offset ' + offset;
    console.log(query2);

    connection2.query(query2, function (err, res) {
        if (err) {
            console.log('query2 err');

        } else {
            if (res.length > 0) {
                // console.log(res);
                artistList = res;
                // var url = '';
                function fn2(url1) {
                    var item = artistList[artistIndex];
                    var artistId = item.id;
                    var url;
                    if (url1) {
                        url = url1;
                    } else {
                        url = `https://music.163.com/artist/album?id=${artistId}&limit=12&offset=0`;
                    }
                    // var url = 'http://www.baidu.com/'
                    console.log(proxy);

                    superagent.get(url)
                        .timeout(3000)
                        .proxy(proxy)
                        .end(function (err, res) {
                            if (err) {
                                console.log('http error');
                                console.log(url);

                                console.log(proxy);
                                console.log();
                                // process.exit();
                                changeIp();
                                fn2();
                            } else {
                                console.log('http success');

                                var text = res.text;

                                var $ = cheerio.load(text);
                                var m_song_module = $('#m-song-module');

                                var list2 = m_song_module.find('li');
                                console.log(list2.length);

                                // var albumName=list2.find('p').text();
                                // console.log(albumName);
                                list2.each((i, element) => {
                                    var item = list2.eq(i);
                                    var name = item.find('p').eq(0).text().replace(/\n/g, '');
                                    console.log(name);
                                    var id = item.find('p>a').eq(0).attr().href.replace(/\/album\?id=/g, '');
                                    console.log(id);
                                    var time = item.find('p').eq(1).text();

                                    var value = [id, name, artistId, time];
                                    console.log(value);
                                    albumList.push(value);
                                });

                                var u_page = $('.u-page');
                                if (u_page.length > 0) {
                                    console.log('u-page >0');
                                    var znxt = $('.znxt').eq(0);
                                    var href = znxt.attr().href;
                                    var url2;
                                    if (href.match(/javascript.?/g)) {
                                        if (artistIndex < artistList.length - 1) {
                                            artistIndex += 1;
                                            fn2();
                                        } else {
                                            console.log('get album end');
                                            console.log(albumList.length);
                                            fn3();
                                        }
                                    } else {
                                        url = 'https://music.163.com' + znxt.attr().href;
                                        console.log('url');
                                        console.log(url);
                                        fn2(url);
                                    }
                                } else {
                                    // 
                                    if (artistIndex < artistList.length - 1) {
                                        artistIndex += 1;
                                        fn2();
                                    } else {
                                        console.log('get album end');
                                        console.log(albumList.length);

                                        console.log(artistId);
                                        var albumListLength = albumList.length;
                                        if (albumListLength > 0) {
                                            fn3();
                                        } else {
                                            fn1(offset + 1);
                                        }

                                    }
                                }


                                function fn3() {
                                    var connection3 = mysql.createConnection(mysqlConfig1);
                                    var query3 = 'insert ignore into album values ?';
                                    connection3.connect();
                                    connection3.query(query3, [albumList], function (err, res) {
                                        if (err) {
                                            console.log('query3 err');
                                            console.log(err);
                                            console.log('albumList');

                                            console.log(albumList);

                                            fn1(offset + 1);
                                        } else {
                                            console.log(res);
                                            artistListUpdated.push(artistId);

                                            fn4();
                                        }
                                    });
                                    connection3.end();
                                }

                                function fn4() {
                                    var connection4 = mysql.createConnection(mysqlConfig1);
                                    var query4 = `UPDATE artist
    SET album_updated = 1 where id in (${artistListUpdated.join(',')});`
                                    console.log(query4);
                                    connection4.query(query4, function (err, res) {
                                        if (err) {
                                            console.log('query4 err');

                                        } else {
                                            console.log('query4 res');
                                            fn1(offset);
                                        }
                                    });
                                    connection4.end();

                                }


                            }
                        })
                }
                fn2();
            } else {
                console.log('no artist');

            }
        }
    });
    connection2.end();
}